package commonInfrastructure.medicalJournal;

import entity.MedicalJournal;
import entity.Staff;
import entity.StaffAccount;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class MedicalJournalSession implements MedicalJournalSessionRemote {
    @PersistenceContext
    private EntityManager em;
    private MedicalJournal mj;
    private Staff staff;
    private String content;
    private List<MedicalJournal> journals;
    
    @Override
    public Long createJournal(String content, String name, String user){
        
        Query query = em.createQuery("SELECT s FROM StaffAccount s WHERE s.staffAccountName = :inStaffAccountName");
        query.setParameter("inStaffAccountName", user);
        
        StaffAccount staffAccount = (StaffAccount) query.getResultList().get(0);
        staff = staffAccount.getStaff();
 
        mj = new MedicalJournal();
        mj.setContent(content);
        mj.setName(name);
        em.persist(mj);
        staff.getMedicalJournal().add(mj);
        em.persist(staff);
        em.flush();
        return mj.getJournalid();
    }
    @Override
    public List<MedicalJournal> listJournal(String user) {
        Query query = em.createQuery("SELECT s FROM StaffAccount s WHERE s.staffAccountName = :inStaffAccountName");
        query.setParameter("inStaffAccountName", user);
        
        StaffAccount staffAccount = (StaffAccount) query.getResultList().get(0);
        staff = staffAccount.getStaff();
 
        journals = new ArrayList<MedicalJournal>();
 
        journals = staff.getMedicalJournal();
        return journals;
    }
    @Override
    public void delete(Long id, String user){
        Query query = em.createQuery("SELECT s FROM StaffAccount s WHERE s.staffAccountName = :inStaffAccountName");
        query.setParameter("inStaffAccountName", user);
        
        StaffAccount staffAccount = (StaffAccount) query.getResultList().get(0);
        staff = staffAccount.getStaff();
        
        MedicalJournal medicalJournal = em.find(MedicalJournal.class, id);
        
        staff.getMedicalJournal().remove(medicalJournal);
        em.remove(medicalJournal);
        em.flush();
    }
    @Override
    public void update( String name, String journal, Long id){
       
        
        MedicalJournal medicalJournal = em.find(MedicalJournal.class, id);
        
        medicalJournal.setName(name);
        medicalJournal.setContent(journal);
        
        em.merge(medicalJournal);
        em.flush();
    }
   
    @Override
    public String getContent() {
        return content;
    }

    @Override
    public void setContent(String content) {
        this.content = content;
    }
    
    @Override
    public MedicalJournal getJournal(Long id){
        MedicalJournal mj = em.find(MedicalJournal.class, id);
        return mj;
    }
}
